﻿using System.Data;
using System.Configuration;
using System.Data.SqlClient;
//sử dụng connection string động dựa vào web.config
namespace DoAn.DAO
{
    public class SqlDataProvider
    {
        public static DataTable LoadData(string sql)
        {
            string cnStr =
                    ConfigurationManager
                        .ConnectionStrings["WEB2"]
                        .ConnectionString;
            SqlConnection cn = new SqlConnection(cnStr);
            SqlDataAdapter da = new SqlDataAdapter(sql, cn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }
        //thực thi câu lệnh (insert,update,delete)
        public static void ExecSql(string sql)
        {
            string cnStr =
                    ConfigurationManager
                        .ConnectionStrings["WEB2"]
                        .ConnectionString;

            SqlConnection cn = new SqlConnection(cnStr);
            SqlCommand cmd = new SqlCommand(sql, cn);
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
        }
        //thực thi câu lệnh select
        public static object ExecSqlScalar(string sql)
        {
            string cnStr =
                    ConfigurationManager
                        .ConnectionStrings["WEB2"]
                        .ConnectionString;

            SqlConnection cn = new SqlConnection(cnStr);
            SqlCommand cmd = new SqlCommand(sql, cn);
            cn.Open();
            object ret = cmd.ExecuteScalar();
            cn.Close();
            return ret;
        }
    }
}